﻿using System;
using System.Web;

namespace School.API
{
    using EDP.Common.Help;
    using EDP.Common.Model;
    using EDP.Framework;

    /// <summary>
    /// 接口基类
    /// </summary>
    public class APIBase : IWebApi
    {
        /// <summary>
        /// 接口调用后业务处理
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public ResultModel AfterInvocationBusiness(HttpContext context) {
            ResultModel ret = new ResultModel();

            //接口调用后需要处理的业务,context中包含所有接口请求信息

            return ret;
        }

        /// <summary>
        /// 接口调用前业务处理
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public ResultModel<bool> BeforeInvocationBusiness(HttpContext context) {
            ResultModel<bool> ret = new ResultModel<bool>();

            //接口调用前需要处理的业务,context中包含所有接口请求信息

            //演示接口调用前记录接口请求日志
            if (context.Request.Headers != null && context.Request.Headers.Count > 0) {
                foreach (string key in context.Request.Headers.AllKeys) {
                    if (key.ToLower() == "apinumbers") {
                        LogHelp.Write("接口调用日志", string.Format("调用接口{0}", context.Request.Headers[key]));  
                        break;
                    }
                }
            }

            ret.Content = true; //返回业务处理结果,如果为false则会抛出错误信息,终止接口请求

            //ret.Content = false;
            //ret.ErrorCode = (int)ErrorCodeEnum.OperateFail; //可定义自己的错误码枚举
            //ret.Message = "操作失败";

            return ret;
        }
    }
}
